package com.botao.tip.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.botao.tip.dao.DepartmentMapper;
import com.botao.tip.dao.UserMapper;
import com.botao.tip.entity.Department;
import com.botao.tip.entity.User;
import com.botao.tip.service.DepartmentService;
import com.botao.tip.service.IUserService;
import com.botao.tip.utils.Md5Util;
import com.botao.tip.utils.UUIDUtil;
import com.botao.tip.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 星时代曹波涛
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class UserserviceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private DepartmentService departmentService;

    @Override
    public User login(String username, String password) {
        //获取盐
        User user = userMapper.selectOne(new QueryWrapper<User>().eq("username", username));
        if (user == null) {
            return null;
        }
        String salt = user.getSalt();
        //验证
        QueryWrapper<User> userQw = new QueryWrapper<>();
        userQw.eq("username", username).eq("password", Md5Util.encodeByMd5(password, salt));
        return userMapper.selectOne(userQw);
    }

    @Override
    public boolean userExists(String username) {
        Integer count = userMapper.selectCount(new QueryWrapper<User>().eq("username", username));
        return count != 0;
    }

    @Override
    public boolean updateUser(String id, String username, String trueName, String gender, String password, String address, String roleId, int departmentId) {
        User user = userMapper.selectOne(new QueryWrapper<User>().eq("id", id));
        user.setUsername(username);
        user.setTrueName(trueName);
        if (!"".equals(password)) {
            String salt = UUIDUtil.create();
            //随机盐
            user.setSalt(salt);
            user.setPassword(Md5Util.encodeByMd5(password, salt));
        }
        user.setAddress(address);
        try {
            user.setGender(Integer.parseInt(gender));
            user.setRole(Integer.parseInt(roleId));
            Department department = departmentService.queryAllById(departmentId);
            user.setDepartmentId(department.getDepartmentId());
        } catch (Exception e) {
            return false;
        }
        return userMapper.updateById(user) != 0;
    }

    @Override
    public Map roleCount() {
        Integer plainUserCount = userMapper.selectCount(new QueryWrapper<User>().eq("role", 0));
        Integer adminCount = userMapper.selectCount(new QueryWrapper<User>().eq("role", 1));
        Integer superAdminCount = userMapper.selectCount(new QueryWrapper<User>().eq("role", 2));
        Integer count = userMapper.selectCount(null);
        Map<Object, Object> map = new HashMap<>();
        map.put("plainUserCount", plainUserCount);
        map.put("adminCount", adminCount);
        map.put("superAdminCount", superAdminCount);
        map.put("count", count);
        return map;
    }

    @Override
    public List<User> userFuzzyQuery(String input) {
        QueryWrapper<User> objectQueryWrapper = new QueryWrapper<User>();
        QueryWrapper<User> like = objectQueryWrapper.like("username", input).or().like("truename", input).or().like("address", input);
        return userMapper.selectList(like);
    }


}
